
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>协程初步使用 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../../java/" />
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../">
            
                <a href="../">
            
                    
                    Android
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" >
            
                <span>
            
                    
                    框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="../框架/BlockCanary.html">
            
                <a href="../框架/BlockCanary.html">
            
                    
                    BlockCanary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2" data-path="../框架/fresco.html">
            
                <a href="../框架/fresco.html">
            
                    
                    Fresco
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.3" data-path="../框架/Glide源码.html">
            
                <a href="../框架/Glide源码.html">
            
                    
                    Glide
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4" data-path="../框架/LeakCanary原理.html">
            
                <a href="../框架/LeakCanary原理.html">
            
                    
                    LeakCanary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.5" data-path="../框架/Retrofit源码.html">
            
                <a href="../框架/Retrofit源码.html">
            
                    
                    Retrofit源码
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    源码解析
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../源码/Binder.html">
            
                <a href="../源码/Binder.html">
            
                    
                    Binder
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../源码/Dialog源码.html">
            
                <a href="../源码/Dialog源码.html">
            
                    
                    Dialog源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../源码/RecycleView缓存机制.html">
            
                <a href="../源码/RecycleView缓存机制.html">
            
                    
                    RecycleView缓存机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.4" data-path="../源码/WMS源码.html">
            
                <a href="../源码/WMS源码.html">
            
                    
                    WMS源码
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" >
            
                <span>
            
                    
                    自定义View及效果
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../自定义View及效果/触摸事件.html">
            
                <a href="../自定义View及效果/触摸事件.html">
            
                    
                    触摸事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../自定义View及效果/自定义ItemDecoration.html">
            
                <a href="../自定义View及效果/自定义ItemDecoration.html">
            
                    
                    自定义ItemDecoration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../自定义View及效果/ItemTouchHelper使用.html">
            
                <a href="../自定义View及效果/ItemTouchHelper使用.html">
            
                    
                    ItemTouchHelper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="../自定义View及效果/MaterilaDesign布局使用.html">
            
                <a href="../自定义View及效果/MaterilaDesign布局使用.html">
            
                    
                    MaterilaDesign布局使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" >
            
                <span>
            
                    
                    Gradle
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../Gradle/Gradle编译流程梳理.html">
            
                <a href="../Gradle/Gradle编译流程梳理.html">
            
                    
                    Gradle编译流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../Gradle/Transform使用.html">
            
                <a href="../Gradle/Transform使用.html">
            
                    
                    Transform使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" >
            
                <span>
            
                    
                    kotlin实践
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.2.5.1" data-path="协程使用.html">
            
                <a href="协程使用.html">
            
                    
                    协程初步使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../java/">
            
                <a href="../../java/">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../java/多线程同步.html">
            
                <a href="../../java/多线程同步.html">
            
                    
                    多线程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../java/垃圾回收机制.html">
            
                <a href="../../java/垃圾回收机制.html">
            
                    
                    垃圾回收机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../计算机网络/">
            
                <a href="../../计算机网络/">
            
                    
                    计算机网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../计算机网络/编码加密.html">
            
                <a href="../../计算机网络/编码加密.html">
            
                    
                    编码加密算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../计算机网络/登陆认证.html">
            
                <a href="../../计算机网络/登陆认证.html">
            
                    
                    登陆认证流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../计算机网络/http协议.html">
            
                <a href="../../计算机网络/http协议.html">
            
                    
                    http协议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../操作系统/">
            
                <a href="../../操作系统/">
            
                    
                    操作系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    设计模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../设计模式/单例模式.html">
            
                <a href="../../设计模式/单例模式.html">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../设计模式/建造者模式.html">
            
                <a href="../../设计模式/建造者模式.html">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >协程初步使用</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="kotlin&#x534F;&#x7A0B;&#x4F7F;&#x7528;">Kotlin&#x534F;&#x7A0B;&#x4F7F;&#x7528;</h2>
<h4 id="&#x5982;&#x4F55;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x534F;&#x7A0B;&#xFF1A;">&#x5982;&#x4F55;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x534F;&#x7A0B;&#xFF1A;</h4>
<p>&#x9996;&#x5148;&#x60F3;&#x8981;&#x8C03;&#x7528;&#x534F;&#x7A0B;&#x65B9;&#x6CD5;&#xFF0C;&#x5FC5;&#x987B;&#x5148;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x5373;&#x534F;&#x7A0B;&#x751F;&#x547D;&#x5468;&#x671F;&#xFF0C;&#x4E00;&#x822C;&#x5728;kotlin&#x4E2D;&#x53EF;&#x4EE5;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x5F00;&#x542F;&#x3002;</p>
<ul>
<li>GlobalScope.launch &#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x534F;&#x7A0B;&#xFF0C;&#x8BE5;&#x534F;&#x7A0B;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x7EBF;&#x7A0B;&#xFF0C;&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x534F;&#x7A0B;&#x4F1A;&#x505C;&#x6B62;&#x6267;&#x884C;&#xFF0C;&#x8BE5;&#x534F;&#x7A0B;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x662F;&#x6574;&#x4E2A;&#x5E94;&#x7528;&#x8FD0;&#x884C;&#x65F6;&#x671F;&#x3002;</li>
<li>runBlocking &#x963B;&#x585E;&#x7EBF;&#x7A0B;&#x5230;&#x8BE5;&#x534F;&#x7A0B;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x3002;
<strong>runBlocking &#x662F;&#x5E38;&#x89C4;&#x51FD;&#x6570;&#xFF0C;&#x800C; coroutineScope &#x662F;&#x6302;&#x8D77;&#x51FD;&#x6570;</strong>
&#x5B9E;&#x9645;&#x4E0A;GlobalScope&#x662F;CoroutineScope&#x7684;&#x5B50;&#x7C7B;&#xFF0C;&#x8BE5;&#x7C7B;&#x662F;&#x6240;&#x6709;&#x534F;&#x7A0B;&#x6784;&#x9020;&#x5668;&#x7684;&#x7236;&#x7C7B;&#xFF0C;&#x7528;&#x4E8E;&#x6269;&#x5C55;&#x4E0D;&#x540C;&#x8303;&#x56F4;&#x7684;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6269;&#x5C55;&#x4E0D;&#x540C;&#x7684;CoroutineScope&#x5B50;&#x7C7B;&#x5B9E;&#x73B0;&#x9002;&#x5E94;Android&#x5404;&#x7C7B;&#x63A7;&#x4EF6;&#x751F;&#x547D;&#x5468;&#x671F;&#x7684;&#x8303;&#x56F4;&#x534F;&#x7A0B;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x8FD9;&#x4E2A;&#x539F;&#x7406;&#x4F1A;&#x5728;&#x540E;&#x9762;&#x7814;&#x7A76;&#xFF0C;&#x73B0;&#x5728;&#x5148;&#x770B;&#x770B;&#x5728;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x7684;&#x534F;&#x7A0B;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<h4 id="&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x7684;&#x534F;&#x7A0B;&#x64CD;&#x4F5C;&#xFF1A;">&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x7684;&#x534F;&#x7A0B;&#x64CD;&#x4F5C;&#xFF1A;</h4>
<p>&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x4E5F;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x662F;&#x4E00;&#x4E2A;&#x9876;&#x5C42;&#x7684;&#x534F;&#x7A0B;&#xFF0C;&#x4EE5;&#x4E0B;&#x7684;&#x64CD;&#x4F5C;&#x7B26;&#x53F7;&#x53EF;&#x4EE5;&#x5728;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x4F7F;&#x7528;</p>
<ul>
<li>launch &#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x4E5F;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x662F;&#x4E00;&#x4E2A;&#x9876;&#x5C42;&#x7684;&#x534F;&#x7A0B;&#xFF0C;&#x8FD9;&#x4E2A;launch&#x8868;&#x793A;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x534F;&#x7A0B;&#x3002;</li>
<li>async &#x5F02;&#x6B65;&#x534F;&#x7A0B;&#xFF0C;&#x8BE5;&#x534F;&#x7A0B;&#x5F00;&#x542F;&#x540E;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x5F00;&#x542F;&#x591A;&#x4E2A;&#x5E76;&#x884C;&#x534F;&#x7A0B;&#x4EFB;&#x52A1;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x663E;&#x793A;&#x8C03;&#x7528;&#x5176;await&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#x963B;&#x585E;&#x540C;&#x6B65;&#x83B7;&#x53D6;&#x534F;&#x7A0B;&#x8FD4;&#x56DE;&#x503C;&#x3002;</li>
<li>withContext &#x8BE5;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x4E0D;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x534F;&#x7A0B;&#x800C;&#x4F7F;&#x7528;&#x4F20;&#x5165;&#x7684;&#x534F;&#x7A0B;&#x4E0A;&#x4E0B;&#x6587;&#x548C;&#x8C03;&#x5EA6;&#x5668;&#x4FEE;&#x6539;&#x534F;&#x7A0B;&#x6240;&#x5904;&#x7684;&#x8FD0;&#x884C;&#x7EBF;&#x7A0B;&#x3002;</li>
</ul>
<h4 id="&#x5BF9;&#x534F;&#x7A0B;&#x7684;&#x540C;&#x6B65;&#x3001;&#x6302;&#x8D77;&#xFF1A;">&#x5BF9;&#x534F;&#x7A0B;&#x7684;&#x540C;&#x6B65;&#x3001;&#x6302;&#x8D77;&#xFF1A;</h4>
<p>&#x6240;&#x6709;&#x7684;&#x534F;&#x7A0B;&#x90FD;&#x90FD;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Job&#x5BF9;&#x8C61;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x53EF;&#x7528;&#x4E8E;&#x5BF9;&#x534F;&#x7A0B;&#x4EFB;&#x52A1;&#x64CD;&#x4F5C;&#x3002;</p>
<ul>
<li>join &#x963B;&#x585E;&#x5F53;&#x524D;&#x7684;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x76F4;&#x5230;Job&#x5BF9;&#x5E94;&#x7684;&#x534F;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6BD5;</li>
<li>cancle &#x53D6;&#x6D88;&#x5BF9;&#x5E94;&#x534F;&#x7A0B;&#x7684;&#x8FDB;&#x884C;</li>
<li>start &#x542F;&#x52A8;&#x534F;&#x7A0B; &#x5982;&#x679C;&#x534F;&#x7A0B;&#x672A;&#x542F;&#x52A8;&#x7684;&#x8BDD;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;true,&#x5426;&#x5219;&#x8FD4;&#x56DE;false</li>
</ul>
<p>&#x7279;&#x6B8A;&#x7684;&#xFF0C;&#x5BF9;&#x4E8E;async&#xFF0C;&#x7531;&#x4E8E;&#x662F;&#x963B;&#x585E;&#x540C;&#x6B65;&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#xFF0C;&#x5176;&#x65B9;&#x6CD5;&#x4F1A;&#x8FD4;&#x56DE;Job&#x7684;&#x5B50;&#x7C7B;Deferred&#xFF0C;&#x8BE5;&#x5B50;&#x7C7B;&#x7684;await&#x65B9;&#x6CD5;&#x53EF;&#x4E0D;&#x963B;&#x585E;&#x7EBF;&#x7A0B;&#x800C;&#x7B49;&#x5F85;&#x534F;&#x7A0B;&#x7684;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x3002;</p>
<h4 id="&#x58F0;&#x660E;&#x6302;&#x8D77;&#x51FD;&#x6570;">&#x58F0;&#x660E;&#x6302;&#x8D77;&#x51FD;&#x6570;</h4>
<ul>
<li>suspend&#xFF1A;&#x53EF;&#x7528;&#x4E8E;&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x8868;&#x793A;&#x8BE5;&#x65B9;&#x6CD5;&#x4F1A;&#x8FD0;&#x884C;&#x5728;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x88AB;&#x6302;&#x8D77;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x8BE5;&#x7EBF;&#x7A0B;&#x4E2D;&#x4F7F;&#x7528;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x7684;&#x64CD;&#x4F5C;&#x7B26;&#x53F7;&#x3002;</li>
</ul>
<h2 id="&#x5404;&#x79CD;&#x534F;&#x7A0B;&#x64CD;&#x4F5C;&#x7B26;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF1A;">&#x5404;&#x79CD;&#x534F;&#x7A0B;&#x64CD;&#x4F5C;&#x7B26;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF1A;</h2>
<ul>
<li>suspendCoroutine &#x5728;&#x6302;&#x8D77;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x7528;&#x4E8E;&#x5C06;&#x5F02;&#x6B65;&#x56DE;&#x8C03;&#x7684;&#x503C;&#x8F6C;&#x5316;&#x4E3A;&#x540C;&#x6B65;&#x503C;&#x8FD4;&#x56DE;&#xFF0C;&#x5728;&#x8C03;&#x7528;resume&#x6216;resumeWithException&#x524D;&#x65B9;&#x6CD5;&#x4F1A;&#x6302;&#x8D77;&#xFF0C;&#x76F4;&#x5230;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x3002;</li>
</ul>
<h2 id="kotlin&#x534F;&#x7A0B;&#x5C01;&#x88C5;&#x4F7F;&#x7528;">Kotlin&#x534F;&#x7A0B;&#x5C01;&#x88C5;&#x4F7F;&#x7528;</h2>
<h4 id="&#x4E00;-&#x3001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;&#x65B9;&#x6CD5;">&#x4E00; &#x3001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;&#x65B9;&#x6CD5;</h4>
<ol>
<li>&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x5C01;&#x88C5;&#xFF1A;&#x9488;&#x5BF9;&#x8017;&#x65F6;&#x4EFB;&#x52A1;&#xFF0C;&#x5982;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#xFF0C;&#x4F7F;&#x7528;suspend&#x58F0;&#x660E;&#x53EF;&#x6302;&#x8D77;&#x3002;<pre><code> private suspend fun delaySomeTime(): String {
     //&#x6A21;&#x62DF;&#x8BF7;&#x6C42;&#x5EF6;&#x65F6;
     delay(100)
     return &quot;suspend return&quot;
 }
</code></pre><ol>
<li>&#x5C01;&#x88C5;&#x8BF7;&#x6C42;&#x51FD;&#x6570;&#x56DE;&#x8C03;:&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x4EFB;&#x52A1;&#xFF0C;&#x518D;&#x56DE;&#x8C03;Success&#x65B9;&#x6CD5;&#x51FD;&#x6570;&#x4E2D;,&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x3002;<pre><code>fun &lt;T&gt; req(method: suspend () -&gt; T, Success: (T) -&gt; Unit, scope: CoroutineScope):Job {
  return scope.launch(Dispatchers.Main) {
      val result = withContext(Dispatchers.IO) {
          method()
      }
      Success(result)
  }
}
</code></pre></li>
<li>&#x4F7F;&#x7528;&#xFF1A;
&#x4F8B;&#x5982;&#x5728;Activity&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x4F20;&#x5165;lifecycleScope&#xFF08;&#x9700;&#x8981;&#x5BFC;&#x5165;&#x5E93;lifecycle-viewmodel-ktx&#xFF09;<pre><code>var job = req(
           { delaySomeTime() },//&#x4F20;&#x5165;&#x8017;&#x65F6;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x7EBF;&#x7A0B;
           { tvResult.text = it },//&#x6210;&#x529F;&#x56DE;&#x8C03;&#x7ED3;&#x679C;
           lifecycleScope
      )
</code></pre></li>
</ol>
</li>
</ol>
<h4 id="&#x4E8C;&#x3001;&#x7B2C;&#x4E8C;&#x79CD;&#x7F51;&#x7EDC;&#x5C01;&#x88C5;&#x65B9;&#x6CD5;&#xFF1A;">&#x4E8C;&#x3001;&#x7B2C;&#x4E8C;&#x79CD;&#x7F51;&#x7EDC;&#x5C01;&#x88C5;&#x65B9;&#x6CD5;&#xFF1A;</h4>
<p> &#x5728;ViewModel&#x4E2D;&#x4F7F;&#x7528;liveData&#x83B7;&#x53D6;suspend&#x65B9;&#x6CD5;&#x7ED3;&#x679C;&#xFF0C;suspedn&#x65B9;&#x6CD5;&#x4E2D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;suspendCoroutine&#x5C06;&#x7ED3;&#x679C;&#x56DE;&#x8C03;&#x5C01;&#x88C5;&#x6210;&#x540C;&#x6B65;&#x83B7;&#x5F97;&#xFF0C;&#x901A;&#x8FC7;liveData&#x53D1;&#x9001;&#x5230;&#x4E0B;&#x6E38;&#x3002;</p>
<ul>
<li><p>1&#x3001;&#x4F7F;&#x7528;suspendCoroutine&#x5C01;&#x88C5;&#x56DE;&#x8C03;&#x4E3A;&#x540C;&#x6B65;&#x6302;&#x8D77;&#xFF1A;</p>
<pre><code>suspend fun getData(): Int {
  return suspendCoroutine { continuation -&gt;
      getInternetData(object : CallBack&lt;Int&gt; {
          override fun success(data: Int) {
              continuation.resume(data)
          }

          override fun error() {
              continuation.resumeWithException(Exception(&quot;error getData&quot;))
          }
      })
  }
}
</code></pre></li>
<li>2&#x3001;&#x901A;&#x8FC7;liveData&#x5EF6;&#x8FDF;&#x53D1;&#x5C04;&#x83B7;&#x53D6;&#x6302;&#x8D77;&#x503C;<pre><code>fun getData2(): LiveData&lt;Int&gt; {
  return liveData(Dispatchers.IO) {
      emit(getData())
  }
}
</code></pre></li>
<li>3&#x3001;&#x5728;ViewModel&#x4E2D;&#x4F7F;&#x7528;liveData&#x6CE8;&#x518C;&#x4E0B;&#x6E38;</li>
</ul>
<h4 id="&#x4E09;&#x3001;&#x7B2C;&#x4E09;&#x79CD;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;&#x65B9;&#x6CD5;&#xFF1A;">&#x4E09;&#x3001;&#x7B2C;&#x4E09;&#x79CD;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;&#x65B9;&#x6CD5;&#xFF1A;</h4>
<p>&#x5C06;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;&#x4E3A;async&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;Deferred&#x5BF9;&#x8C61;&#xFF0C;&#x518D;&#x901A;&#x8FC7;await&#x65B9;&#x6CD5;&#x540C;&#x6B65;&#x83B7;&#x5F97;&#x7ED3;&#x679C;</p>
<ul>
<li>1&#x3001;&#x5C01;&#x88C5;Deferred<pre><code>fun getData3(): Deferred&lt;Int&gt; {
  val deferred = CompletableDeferred&lt;Int&gt;()
  getInternetData(object : CallBack&lt;Int&gt;{
      override fun success(data: Int) {
          deferred.complete(data)
      }
      override fun error() {
          deferred.completeExceptionally(Exception(&quot;error&quot;))
      }
  })
  return deferred
}
</code></pre></li>
<li>2&#x3001;&#x5728;&#x534F;&#x7A0B;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x8C03;&#x7528;&#x83B7;&#x5F97;&#x7ED3;&#x679C;<pre><code>getData3().await())
</code></pre></li>
</ul>
<!--## Kotlin中协程原理： 通过将多个挂起区域拆分成多个函数块，使用label对每个代码块标记代码执行的状态，通过改变label状态机结合goto指令让线程达到切换线程后回来后在制定挂起函数后面执行代码-->
<h4 id="&#x534F;&#x7A0B;&#x6E90;&#x7801;&#x89E3;&#x6790;">&#x534F;&#x7A0B;&#x6E90;&#x7801;&#x89E3;&#x6790;</h4>
<ul>
<li>Kotlin &#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x5BF9; suspending &#x65B9;&#x6CD5;&#x505A;&#x7279;&#x6B8A;&#x5904;&#x7406;&#xFF0C;&#x5BF9;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x8F6C;&#x6362;&#xFF0C;&#x4ECE;&#x800C;&#x5B9E;&#x73B0; suspending &#x673A;&#x5236;<ul>
<li>&#x589E;&#x52A0;&#x4E00;&#x4E2A;Continuation &#x7C7B;&#x578B;&#x5165;&#x53C2;&#xFF0C;&#x5C06;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x53D8;&#x4E3A; Object&#x5373;Any?</li>
<li>&#x751F;&#x6210; Continuation &#x7C7B;&#x578B;&#x7684;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;</li>
<li>&#x5BF9; suspending &#x65B9;&#x6CD5;&#x7684;&#x8C03;&#x7528;&#x53D8;&#x4E3A; switch &#x5F62;&#x5F0F;&#x7684;&#x72B6;&#x6001;&#x673A;&#x3002;</li>
</ul>
</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="../../java/" class="navigation navigation-next navigation-unique" aria-label="Next page: Java">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"layout":"post","title":"协程初步使用","subtitle":" 协程初步使用","date":"2020-06-19T21:15:00.000Z","author":"hzy","//header-img":"img/post-bg-2015.jpg","catalog":true,"tags":["kotlin"],"level":"1.2.5.1","depth":3,"next":{"title":"Java","level":"1.3","depth":1,"path":"java/README.md","ref":"java/README.md","articles":[{"title":"多线程同步","level":"1.3.1","depth":2,"path":"java/多线程同步.md","ref":"java/多线程同步.md","articles":[]},{"title":"垃圾回收机制","level":"1.3.2","depth":2,"path":"java/垃圾回收机制.md","ref":"java/垃圾回收机制.md","articles":[]}]},"previous":{"title":"kotlin实践","level":"1.2.5","depth":2,"ref":"","articles":[{"title":"协程初步使用","level":"1.2.5.1","depth":3,"path":"android/Kotlin/协程使用.md","ref":"android/Kotlin/协程使用.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"android/Kotlin/协程使用.md","mtime":"2020-08-16T16:13:19.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-13T16:08:39.790Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

